package com.bjsxt.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.CreateCache;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.domain.CashRecharge;

import com.bjsxt.dto.UserDto;
import com.bjsxt.feign.UserServiceFeign;
import com.bjsxt.mapper.CashRechargeAuditRecordMapper;
import com.bjsxt.model.CashParam;
import com.bjsxt.service.AccountService;
import com.bjsxt.service.CoinService;
import com.bjsxt.service.ConfigService;
import com.bjsxt.vo.CashTradeVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjsxt.mapper.CoinRechargeMapper;
import com.bjsxt.domain.CoinRecharge;
import com.bjsxt.service.CoinRechargeService;
import org.springframework.util.CollectionUtils;

@Service
public class CoinRechargeServiceImpl extends ServiceImpl<CoinRechargeMapper, CoinRecharge> implements CoinRechargeService{



    @Autowired
    private UserServiceFeign userServiceFeign;



    @Autowired
    private ConfigService configService;



    @Autowired
    private CoinService coinService;

    @Autowired
    private Snowflake snowflake;

    @CreateCache(name = "CASH_RECHARGE_LOCK:", expire = 100, timeUnit = TimeUnit.SECONDS, cacheType = CacheType.BOTH)
    private Cache<String, String> cache;


    @Autowired
    private CashRechargeAuditRecordMapper cashRechargeAuditRecordMapper;


    @Autowired
    private AccountService accountService;


    @Override
    public Page<CoinRecharge> findByPage(Page<CoinRecharge> page, Long userId, String userName, String mobile, Byte status, String numMin, String numMax, String startTime, String endTime) {
        Map<Long, UserDto> basicUsers = null;
        LambdaQueryWrapper<CoinRecharge> cashWithdrawalsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(userId !=null || !StringUtils.isEmpty(userName)|| !StringUtils.isEmpty(mobile)){
            basicUsers = userServiceFeign.getBasicUsers(userId == null ? null : Arrays.asList(userId), userName, mobile);
            if(CollectionUtils.isEmpty(basicUsers)){
                return page;
            }
            Set<Long> userids = basicUsers.keySet();
            cashWithdrawalsLambdaQueryWrapper.in(CoinRecharge::getUserId,userids);

        }

        cashWithdrawalsLambdaQueryWrapper.eq(status!=null,CoinRecharge::getStatus,status)
                .between(!StringUtils.isEmpty(numMin)||!StringUtils.isEmpty(numMax),CoinRecharge::getAmount,
                        new BigDecimal(!StringUtils.isEmpty(numMin)?"0":numMin),
                        new BigDecimal(!StringUtils.isEmpty(numMax)?"0":numMax) )
                .between(!StringUtils.isEmpty(startTime)||!StringUtils.isEmpty(endTime),CoinRecharge::getCreated,
                        startTime,
                        endTime+"23:59:59" );
        Page<CoinRecharge> pageDate = page(page, cashWithdrawalsLambdaQueryWrapper);
        List<CoinRecharge> records = pageDate.getRecords();
        if(!CollectionUtils.isEmpty(records)){
            List<Long> userIds = records.stream().map(CoinRecharge::getUserId).collect(Collectors.toList());
            if(basicUsers == null){
                basicUsers = userServiceFeign.getBasicUsers(userIds, null, null);
            }

            Map<Long, UserDto> finalBasicUsers = basicUsers;
            records.forEach(coinRecharge -> {
                UserDto userDto = finalBasicUsers.get(coinRecharge.getUserId());
                if(userDto!=null){
                    coinRecharge.setRealName(userDto.getRealName());
                    coinRecharge.setUsername(userDto.getRealName());
                }

            });

        }


        return pageDate;
    }

    @Override
    public Page<CoinRecharge> findUserCoinRecharge(Page<CoinRecharge> page, Long coinId, Long userId) {
        return page(page,new LambdaQueryWrapper<CoinRecharge>().eq(CoinRecharge::getCoinId,coinId)

                .eq(CoinRecharge::getUserId,userId)

        );
    }


}
